﻿using Prism.Commands;
using Prism.Services.Dialogs;
using SmartPlant.Core;
using SmartPlant.Core.Mvvm;
using SmartPlant.Model.Biz;
using SmartPlant.Services;
using SmartPlant.Services.Interfaces;
using SmartPlant.WinCore;
using System.Collections.ObjectModel;
using System.Windows.Input;
using Unity;

namespace SmartPlant.Modules.Base.ViewModels
{
    public class CodelistSelectViewModel : DialogViewModelBase, IDialogAware
    {
        private ObservableCollection<DropDownListObj> _dropDownList;
        /// <summary>
        /// 协助搜索用
        /// </summary>
        private ObservableCollection<DropDownListObj> _dropDownAllList;
        private DelegateCommand<object> _searchCommand;
        private string _searchText;
        private DropDownListObj _selectObj;
        private ICodeListService _codelistServ;

        public CodelistSelectViewModel(IDialogService dialogService, IUnityContainer unityContainer) : base(dialogService)
        {
            this.Title = "下拉列表选择";
            _dropDownList = new ObservableCollection<DropDownListObj>();
            _dropDownAllList = new ObservableCollection<DropDownListObj>();
            _codelistServ = unityContainer.Resolve<CodeListService>();
        }

        #region binding-data
        /// <summary>
        /// 列表
        /// </summary>
        public ObservableCollection<DropDownListObj> DropDownListObjs
        {
            get
            {
                return _dropDownList;
            }
        }
        /// <summary>
        /// 搜索栏的文本
        /// </summary>
        public string SearchText
        {
            get { return _searchText; }
            set { SetProperty(ref _searchText, value); }
        }
        /// <summary>
        /// LIST里选择的item
        /// </summary>
        public DropDownListObj SelectObj
        {
            get { return _selectObj; }
            set { SetProperty(ref _selectObj, value); }
        }
        #endregion
        #region command
        /// <summary>
        /// 点击搜索按钮后，触发
        /// </summary>
        public ICommand SearchCommand
        {
            get
            {
                if (_searchCommand != null) return _searchCommand;
                _searchCommand = new DelegateCommand<object>(onSearchCommandCommand);
                return _searchCommand;
            }
        }
        #endregion
        private void onSearchCommandCommand(object obj)
        {
            if (SearchText.IsNullOrEmpty())
            {
                DropDownListObjs.Clear();
                DropDownListObjs.AddRange(_dropDownAllList);
                return;
            }
            var result = from v in _dropDownAllList
                         where v.Name.Contains(SearchText)
                         select v;

            DropDownListObjs.Clear();
            DropDownListObjs.AddRange(result);
        }

        /// <summary>
        /// 关闭窗口后。xaml绑定的是 <see cref="DialogViewModelBase.CloseDialogCommand"/>
        /// </summary>
        /// <returns></returns>
        protected override DialogResult RaiseDialogOk()
        {
            if (_selectObj.IsNullOrEmpty())
            {
                Dialog.ShowAltert("未选择任何选择项！！！");
                return new DialogResult(ButtonResult.Cancel);
            }
            return new DialogResult(ButtonResult.OK, new DialogParameters($"id={_selectObj.Id}&name={_selectObj.Name}"));
        }

        /// <summary>
        /// 对话框打开时，触发。由prism的dialog机制来决定
        /// </summary>
        /// <param name="parameters"></param>
        public override async void OnDialogOpened(IDialogParameters parameters)
        {
            var itemId = parameters.GetValue<decimal>("itemId");
            Title = parameters.GetValue<string>("title");
            var curValue = parameters.GetValue<string>("curValue");
            var list = await _codelistServ.GetDropDownListByAttrAsync(itemId);// GlobalObject.GetDropDownListByAttr(itemId);
            if (list != null)
            {
                DropDownListObjs.Clear();
                DropDownListObjs.AddRange(list);
                _dropDownAllList.Clear();
                _dropDownAllList.AddRange(list);

                var curEnum = DropDownListObjs.FirstOrDefault(x => x.Name == curValue);
                if (curEnum != null)
                {
                    SelectObj = curEnum;
                }
            }
        }
    }
}
